/****************************************************************************
	
	Date: July, 2022
	
	Program: Export data for baseline year into CSV
	
	Output:
	- Various CSV files later used in Matlab (saved in $datacsvmatlab)
		
	
****************************************************************************/


cd "$outputpath"
use "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_$year", clear
keep if year==$year
keep siren year wiot labor_share
save alpha_${sample}_$yearactual, replace
sort wiot12 siren
keep labor_share
export delimited "$datacsvmatlab/labor_pi_${method}_${sample}.csv", replace
use alpha_${sample}_$yearactual, clear
keep siren wiot
sort wiot siren
order siren wiot
export delimited "$datacsvmatlab/WiOT_data/wiot12_jp_${method}_${sample}.csv", replace
use "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_$year", clear
keep if year==$year
keep siren year wiot pi*
save pi_${sample}_$yearactual, replace
sort wiot12 siren
keep pi*
order pi_nkjptAUS pi_nkjptAUT pi_nkjptBEL pi_nkjptBGR pi_nkjptBRA pi_nkjptCAN pi_nkjptCHN pi_nkjptCYP pi_nkjptCZE pi_nkjptDEU pi_nkjptDNK pi_nkjptESP pi_nkjptEST pi_nkjptFIN pi_nkjptFRA pi_nkjptGBR pi_nkjptGRC pi_nkjptHUN pi_nkjptIDN pi_nkjptIND pi_nkjptIRL pi_nkjptITA pi_nkjptJPN pi_nkjptKOR pi_nkjptLTU pi_nkjptLVA pi_nkjptMEX pi_nkjptMLT pi_nkjptNLD pi_nkjptPOL pi_nkjptPRT pi_nkjptROU pi_nkjptRUS pi_nkjptSVK pi_nkjptSVN pi_nkjptSWE pi_nkjptTUR pi_nkjptTWN pi_nkjptUSA pi_nkjptROW
export delimited "$datacsvmatlab/Firm_level_France_pi_${method}_${sample}.csv", replace

use "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_$year", clear
keep if year==$year
keep siren year wiot homo_pi*
save homogeneous_pi_${sample}_$yearactual, replace
use "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_$year", clear
keep if year==$year

gen sum_hom_gammas = 0
gen sum_het_gammas = 0
foreach i in $wiotcountry_FRA {
	foreach j in $wiotsector_ALLXJP {	
		replace sum_hom_gammas = sum_hom_gammas + homo_gamma_`i'`j'
		replace sum_het_gammas = sum_het_gammas + gamma_`i'`j'
	}
}
foreach i in $wiotcountry_FRA {
	foreach j in $wiotsector_ALLXJP {	
		replace homo_gamma_`i'`j' = homo_gamma_`i'`j'/sum_hom_gammas
		replace gamma_`i'`j' = gamma_`i'`j'/sum_het_gammas
	}
}


foreach j in $wiotcountry_FRA{
	preserve
	keep siren year wiot gamma_`j'*
	save gamma_`j'_${sample}_$yearactual, replace
	sort wiot12 siren
	drop siren year wiot
	order gamma_`j'AtB gamma_`j'C gamma_`j'15t16 gamma_`j'17t18 gamma_`j'19 gamma_`j'20 gamma_`j'21t22 gamma_`j'23 gamma_`j'24 gamma_`j'25 gamma_`j'26 gamma_`j'27t28 gamma_`j'29 gamma_`j'30t33 gamma_`j'34t35 gamma_`j'36t37 gamma_`j'E gamma_`j'F gamma_`j'50 gamma_`j'51 gamma_`j'52 gamma_`j'H gamma_`j'60 gamma_`j'61 gamma_`j'62 gamma_`j'63 gamma_`j'64 gamma_`j'70 gamma_`j'71t74 gamma_`j'M gamma_`j'N gamma_`j'O
	export delimited "$datacsvmatlab/gamma/Firm_level_France_gamma_${sample}_`j'.csv", replace
	restore
}
foreach j in $wiotcountry_FRA{
	preserve
	keep siren year wiot homo_gamma_`j'*
	save homogeneous_gamma_`j'_${sample}_$yearactual, replace
	sort wiot12 siren
	drop siren year wiot
	order homo_gamma_`j'AtB homo_gamma_`j'C homo_gamma_`j'15t16 homo_gamma_`j'17t18 homo_gamma_`j'19 homo_gamma_`j'20 homo_gamma_`j'21t22 homo_gamma_`j'23 homo_gamma_`j'24 homo_gamma_`j'25 homo_gamma_`j'26 homo_gamma_`j'27t28 homo_gamma_`j'29 homo_gamma_`j'30t33 homo_gamma_`j'34t35 homo_gamma_`j'36t37 homo_gamma_`j'E homo_gamma_`j'F homo_gamma_`j'50 homo_gamma_`j'51 homo_gamma_`j'52 homo_gamma_`j'H homo_gamma_`j'60 homo_gamma_`j'61 homo_gamma_`j'62 homo_gamma_`j'63 homo_gamma_`j'64 homo_gamma_`j'70 homo_gamma_`j'71t74 homo_gamma_`j'M homo_gamma_`j'N homo_gamma_`j'O
	export delimited "$datacsvmatlab/gamma/Firm_level_France_hom_gamma_${sample}_`j'.csv", replace
	restore
}
use "$outputpath/Firm_Level_pi_alpha_gamma_${method}_${sample}_$year", clear
keep if year==$year
bysort wiot12: egen industry_inputs = sum(firm_inputs)
gen weight = firm_inputs/industry_inputs
foreach i in $wiotcountry_FRA{
	foreach j in $wiotsector_ALLXJP{
		replace gamma_`i'`j' = weight*gamma_`i'`j'
		bys wiot: egen alt_hom_`i'`j'=sum(gamma_`i'`j')
	}
}
foreach j in $wiotcountry_FRA{
	preserve
	keep siren year wiot alt_hom_`j'*
	save alt_hom_`j'_${sample}_$yearactual, replace
	sort wiot12 siren
	drop siren year wiot
	order alt_hom_`j'AtB alt_hom_`j'C alt_hom_`j'15t16 alt_hom_`j'17t18 alt_hom_`j'19 alt_hom_`j'20 alt_hom_`j'21t22 alt_hom_`j'23 alt_hom_`j'24 alt_hom_`j'25 alt_hom_`j'26 alt_hom_`j'27t28 alt_hom_`j'29 alt_hom_`j'30t33 alt_hom_`j'34t35 alt_hom_`j'36t37 alt_hom_`j'E alt_hom_`j'F alt_hom_`j'50 alt_hom_`j'51 alt_hom_`j'52 alt_hom_`j'H alt_hom_`j'60 alt_hom_`j'61 alt_hom_`j'62 alt_hom_`j'63 alt_hom_`j'64 alt_hom_`j'70 alt_hom_`j'71t74 alt_hom_`j'M alt_hom_`j'N alt_hom_`j'O
	export delimited "$datacsvmatlab/gamma/Firm_Level_France_alt_hom_gamma_${sample}_`j'.csv", replace
	restore
}
